<template>
  <div class="my-tab-bar">
    <div
      class="tab-item"
      v-for="(item, index) in arr"
      :key="index"
      @click="changeTap(index)"
      :class="{ current: index === targetIndex }"
    >
      <!-- 图标 -->
      <span class="iconfont" :class="item.iconText"></span>
      <!-- 文字 -->
      <span>{{ item.text }}</span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    arr: {
      type: Array,
      // 设置必传
      require: true,
      // 自定义校验规则
      validator(value) {
        // console.log(value);
        if (value.length < 2 || value.length > 5) {
          console.error('prop arr length must 2-5')
          return false
        }
        return true
      },
    },
  },
  data() {
    return {
      targetIndex: 0,
    }
  },
  methods: {
    // 点击切换
    changeTap(index) {
      // 点击获取下标
      this.targetIndex = index
      // 接收自定义事件,点击切换组件
      // this.$emit('changeCom',cname)
      if (index === 0) {
        this.$router.push({
          path: '/',
        })
      } else if (index === 1) {
        this.$router.push({
          path: '/search',
        })
      } else {
        this.$router.push({
          path: '/userInfo',
        })
      }
    },
  },
}
</script>

<style lang="less" scoped>
.my-tab-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: white;
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
}

.current {
  color: #2299c5;
}
</style>
